package com.pyh.libray.log;

import android.graphics.drawable.Icon;
import android.util.Log;

import androidx.annotation.NonNull;

import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;

public class PLog {
    public static void v(Object... contents) {
        log(PLogType.V, contents);
    }

    public static void vt(@NonNull String tag, Object... contents) {
        log(PLogType.V, tag, contents);
    }

    public static void d(Object... contents) {
        log(PLogType.D, contents);
    }

    public static void dt(@NonNull String tag, Object... contents) {
        log(PLogType.D, tag, contents);
    }

    public static void i(Object... contents) {
        log(PLogType.I, contents);
    }

    public static void it(@NonNull String tag, Object... contents) {
        log(PLogType.I, tag, contents);
    }

    public static void w(Object... contents) {
        log(PLogType.W, contents);
    }

    public static void wt(@NonNull String tag, Object... contents) {
        log(PLogType.W, tag, contents);
    }

    public static void e(Object... contents) {
        log(PLogType.E, contents);
    }

    public static void et(@NonNull String tag, Object... contents) {
        log(PLogType.E, tag, contents);
    }

    public static void a(Object... contents) {
        log(PLogType.A, contents);
    }

    public static void at(@NonNull String tag, Object... contents) {
        log(PLogType.A, tag, contents);
    }

    public static void log(@PLogType.TYPE int type, Object... content) {
        log(type, PLogManager.getInstance().getpLogConfig().getLogTag(), content);
    }

    public static void log(@PLogType.TYPE int type, @NonNull String tag, Object... content) {
        log(PLogManager.getInstance().getpLogConfig(), type, tag, content);
    }

    public static void log(PLogConfig config, @PLogType.TYPE int type, @NonNull String tag, Object... content) {
        if (!config.enable()) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        if (config.includeThred()) {
            String threadInfo = PLogConfig.P_THREAD_FORMATTER.formatter(Thread.currentThread());
            sb.append(threadInfo).append("\n");
        }
        if (config.staceTraceDeep() > 0) {
            String stackTrace = PLogConfig.P_STACKTRACE_FORMATTER.formatter(new Throwable().getStackTrace());
            sb.append(stackTrace).append("\n");
        }
        parseBody(sb, content);
        List<PLogPrinter> printers = config.printers() != null ? Arrays.asList(config.printers()) : PLogManager.getInstance().getPrinters();
        if (printers==null){
            return;
        }
        for (PLogPrinter printer : printers) {
            printer.print(config,type,tag,sb.toString());
        }
    }

    private static void parseBody(StringBuilder sb, Object... contents) {
        for (Object content : contents) {
            sb.append(content.toString()).append(";");
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
    }

}
